package com.liuqinqi.barter.common.serverResponse;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.liuqinqi.barter.common.enums.ResponseCodeEnum;
import com.liuqinqi.barter.exception.ServiceException;
import com.liuqinqi.barter.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;

@SuppressWarnings("serial")
public class RequestDTO extends LinkedHashMap<String,Object>{

	/**
	 * 校验非空参数
	 * @param str
	 * @param paramName
	 * @param <T>
	 */
	public static  <T> void checkRequiredParam(T str, String paramName) {
		if (str instanceof String && StringUtils.isBlank((String)str)) {
			throw new ServiceException(ResponseCodeEnum.REQUIRED_ARGUMENT.getCode(), ResponseCodeEnum.REQUIRED_ARGUMENT.getDesc() + paramName + ", 参数类型为字符型, 且不能为空");
		} else if (str instanceof List && ((List<?>) str).size() <= 0) {
			throw new ServiceException(ResponseCodeEnum.REQUIRED_ARGUMENT.getCode(), ResponseCodeEnum.REQUIRED_ARGUMENT.getDesc() + paramName + ", 参数类型为数组, 且不能为空");
		} else if (str instanceof BigDecimal && ((BigDecimal) str).compareTo(BigDecimal.ZERO) == -1) {
			throw new ServiceException(ResponseCodeEnum.REQUIRED_ARGUMENT.getCode(), ResponseCodeEnum.REQUIRED_ARGUMENT.getDesc() + paramName + ", 参数最多保留小数点后两位, 且不能为空");
		} else if (null == str) {
			throw new ServiceException(ResponseCodeEnum.REQUIRED_ARGUMENT.getCode(), ResponseCodeEnum.REQUIRED_ARGUMENT.getDesc() + paramName + ", 参数类型为整型, 且不能为空");
		}
	}
	
	public String getParam(String key) {
		if (!containsKey(key)) {
			return null;
		}
		try {
			return get(key).toString();
		} catch (Exception e) {
			throw new ServiceException(ResponseCodeEnum.ERROR.getCode(), "参数错误。期望字符型, 错误的参数为：" + key);
		}
	}
	
	public Integer getIntParam(String key) {
		if (!containsKey(key)) {
			return null;
		}
		try {
			return Integer.parseInt(get(key).toString());
		} catch (Exception e) {
			throw new ServiceException(ResponseCodeEnum.ERROR.getCode(), "参数错误。期望整型, 错误的参数为：" + key);
		}
	}
	
	public JsonArray getArrayParm(String key){
		if (!containsKey(key)) {
			return null;
		}
		try {
			return new JsonParser().parse(get(key).toString()).getAsJsonArray();
		} catch (Exception e) {
			throw new ServiceException(ResponseCodeEnum.ERROR.getCode(), "参数错误。期望数组对象, 错误的参数为：" + key);
		}
	}
	
	public <T> List<T> getListParam(String key, Class<T> clazz) {
		if (!containsKey(key)) {
			return null;
		}
		
		try {
			return JsonUtil.fromJsonToList(JsonUtil.toJson(get(key)), clazz);
		} catch (Exception e) {
			throw new ServiceException(ResponseCodeEnum.ERROR.getCode(), "参数错误。期望数组, 错误的参数为：" + key);
		}
	}
	
	public <T> T getParam(Class<T> clazz) {
		try {
			return JsonUtil.fromJsons(JsonUtil.toJson(this), clazz);
		} catch (Exception e) {
			throw new ServiceException(ResponseCodeEnum.ERROR.getCode(), "参数错误: " + clazz);
		}
	}
	
	public <T> T getObjectParam(String key, Class<T> clazz) {
		try {
			return JsonUtil.fromJsons(JsonUtil.toJson(get(key)), clazz);
		} catch (Exception e) {
			throw new ServiceException(ResponseCodeEnum.ERROR.getCode(), "参数错误, 期望对象:" + clazz);
		}
	}
}
